<?php
header("Content-type: text/html; charset=utf-8"); 

//在指定的字符前添加反斜杠的字符串
$str = "Welcome to my humble Homepage!";
echo $str."<br>";
echo addcslashes($str,'m')."<br>";
echo addcslashes($str,'H')."<br>";
echo addcslashes($str,'A..Z')."<br>";
echo addcslashes($str,'a..z')."<br>";
echo addcslashes($str,'a..g')."<br><br>";


//单引号可用的转义字符是 \
//在预定义的字符前添加反斜杠的字符串
$str = "Who's Peter Griffin?";
$str1 =' Who\'s Peter Griffin?'; //转义单引号
echo $str . " This is not safe in a database query.<br>";
echo $str1 . " This is not safe in a database query.<br>"; //Who's Peter Griffin? This is not safe in a database query.
echo addslashes($str) . " This is safe in a database query."."<br><br>";
//转义反斜杠
$path = 'C:\\WINDOWS';
$path1 = "C:\\WINDOWS";
echo  $path."<br><br>";
echo  $path1."<br><br>";

$nope = '\n'; //不是一个转义
echo "${nope}<br><br>";


//双引号括起来的字符串允许插值并且支持多种转义序列。
$str = "hello \n world \n\r\$\[\]";
echo $str;


//把一个字符串值从二进制转换为十六进制，再转换回去
$str = "Hello world!";
echo bin2hex($str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br><br>";

//移除字符串右侧的字符
$str = "我是phper 哈哈!";
echo $str . "<br>";
echo chop($str,"哈哈!"). "<br><br>";


$str = "活捉一条很长很长的新闻!";
echo chunk_split($str,20,"..."). "<br><br>";


$who = 'Kilroy';
$where = 'here';
echo "$who was $where". "<br><br>";
//单引号括起来的字符串是不能被解析的
echo '$who was $where'. "<br><br>";

$n = 12;
echo "You are the {$n}th person". "\r\n<br><br>";


//here 文档
/*header('Content-Type:test/plain;');
$clerihew = <<< EndofQuote 
sir humphrey davy abominated gravy he lived in the odium of having discouvered sodium. 
EndofQuote;
echo $clerihew;*/


/*
 * 输出字符串
 * 1. echo 结构一次可以输出许多值
 * 2. print() 函数一次只能输出一个值
 * 3. printf() 函数能够根据模版格式化输出字符串
 * 4. print_r() 函数有利于调试--它能更容易读懂的方式打印数组、对象和其他内容。
 */


/*
 * 编码和转义
 * 1. HTML 页面; eg:字面值小于号(<)转义为 &lt;
 * 2. Web 地址(URL); eg:空格 转义为 %20
 * 3. 数据库交互;
 */

//HTML 中对特殊字符以尸体字符表示&amp;(&)、&lt;(<)


//对所有特殊字符进行实体引用

/*
 * 函数htmlentities（） 将HTML 字符转换成对应的实体（除了空格符），包括小于号（＜）、大于号（＞）、and 符号（＆）和重音字符。
 * 默认是ENT_COMPAT(默认)只转换双引号
 * ENT_QUOTES 两种引号都转换
 * ENT_NOQUOTES 两种引号都不转换
*/
$str = "Einstürzende Neubauten ";
$str1 = htmlentities("Einstürzende Neubauten "); 
echo $str,$str1. "\r\n<br><br>";

$str2 = "Stop pulling my hair! & jane's eyes flashed. <p>";
$double = htmlentities($str2);//Stop pulling my hair! &amp; jane's eyes flashed. &lt;p&gt;
$both = htmlentities($str2,ENT_QUOTES); //Stop pulling my hair! &amp; jane&#039;s eyes flashed. &lt;p&gt;
$neither = htmlentities($str2,ENT_NOQUOTES);//Stop pulling my hair! &amp; jane's eyes flashed. &lt;p&gt;
echo $str2. "\r\n<br><br>"
,$double. "\r\n<br><br>"
,$both. "\r\n<br><br>"
,$neither. "\r\n<br><br>";
var_dump($double ,$both,$neither);

//-----只对HTML 语法字符串进行实体引用
/*
 * 函数htmlspecialchars() 转换最小的实体集来生成合法的HTML。
 * input
 * flags
 * endcoding
*/
//$str = "aaa Π bbb";
$table = get_html_translation_table(HTML_ENTITIES);
//var_dump($table);
$revTrans = array_flip($table);
//var_dump($revTrans);
echo strstr($str1, "&uuml;");


/*$table = get_html_translation_table(HTML_ENTITIES);
$table[' '] = '&nbsp;';
$encoded = strstr($original,$table);*/


//删除 HTML 标签
/*
 * 函数 strip_tags() 从字符串中删除HTML 标签：
*/

$input = '<p>Howdy,&quot; Cowboy&quot;</p>';
$output = strip_tags($input);
var_dump($output);


//提取元标签
/*
 * 函数 get_meta_tags() 可返回包含该页面中元标签(meta tag)内容的数组。
*/
//$metaTags = get_meta_tags('https://www.dfrobot.com/');
//echo "web page made by {$metaTags['keywords']}". "\r\n<br><br>";


//-----URL
/*
 * RFC 3986 编码和解码
 * rawurlencode() :接收一个字符串并返回一个把非法URL中的字符转换成按%dd 约定编码的字符串副本
 * rawurldecode() :用于解码被编译过的URL 字符串
*/

$name = "Programming PHP/C++&MySQL";
$output = rawurlencode($name);
echo "http://localhost/{$output}". "\r\n<br><br>";

$encoded = 'http://localhost/Programming%20PHP';
echo rawurldecode($encoded). "\r\n<br><br>";


/*
 * 查询字符串编码
 * urlencode() : 它们把空格编码为加号(+)。而不是%20
 * urldecode() :用于解码被编译过的URL 字符串
*/
$urlendoce_rst = urlencode($name);
echo $urlendoce_rst ;
echo urldecode($urlendoce_rst );



//-----SQL
/*
 * 绝大多数数据库都要求对SQL 查询字符串进行转义。
 * addslashes() : 在单引号、双引号、空字符串和反斜杠前面加一个反斜杠
 * stripslashes() :删除反斜杠
*/
$sql_string = "he 'llo' \ world ";
echo addslashes($sql_string);
echo stripcslashes(addslashes($sql_string));


//-----比较字符串
//精确比较
$o1 = 3;
$o2 = '3';

if($o1 == $o2){
    echo("== retruns true<br>");
}

if($o1 === $o2){
    echo("=== retruns true<br>");
}

$string = "PHP Rocks";
$number = 5;
if($string < $number){
    echo("{$string} < {$number}<br>");
}

$n = strcmp($string,$number);
echo $n."<br>";


//PHP 提供了一些函数来测试两个字符串是否近似相等
$know = "Fred";
$query = "Phred";

if(soundex($know) == soundex($query)){
    echo "yes";
}else{
    echo "no";
}


if(metaphone($know) == metaphone($query)){
    echo "yes";
}else{
    echo "no";
}

$string1 = "中国人民共和国";//"Rasmus Lerdorf";
$string2 = "中华人民共和国";//"Razmus Lehrdorf";
$com = similar_text($string1,$string2,$percent);
printf("They had %d chars in common(%.2f%%). ",$com,$percent);